﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Net;

namespace Mock51
{
    class Program
    {
        static void Main(string[] args)
        {
            WebRequestHandler handler = new WebRequestHandler();
            handler.UseCookies = true;
            handler.CookieContainer = new System.Net.CookieContainer();
            handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            HttpClient client = new HttpClient(handler);
            var get = client.GetStringAsync("http://ehire.51job.com/MainLogin.aspx").Result;

            string sc = string.Empty;
            string oldAccessKey = string.Empty;
            string ec = string.Empty;

            Regex regex = new Regex(@"<input type=""hidden"" name=""fksc"" id=""fksc"" value=""(?<sc>\S+?)"" />", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline | RegexOptions.Singleline);
            Match match = regex.Match(get);
            if (match.Success)
            {
                sc = match.Groups["sc"].Value;
            }

            regex = new Regex(@"<input type=""hidden"" name=""hidAccessKey"" id=""hidAccessKey"" value=""(?<oldacc>\S+?)"" />", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline | RegexOptions.Singleline);
            match = regex.Match(get);
            if (match.Success)
            {
                oldAccessKey = match.Groups["oldacc"].Value;
            }

            regex = new Regex(@"<input type=""hidden"" name=""hidEhireGuid"" id=""hidEhireGuid"" value=""(?<ec>\S+?)"" />", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline | RegexOptions.Singleline);
            match = regex.Match(get);
            if (match.Success)
            {
                ec = match.Groups["ec"].Value;
            }

            client.DefaultRequestHeaders.Add("Referer", "http://ehire.51job.com/MainLogin.aspx");
            client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36");
            client.DefaultRequestHeaders.Add("Host", "ehirelogin.51job.com");
            client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip,deflate");
            client.DefaultRequestHeaders.Add("Accept-Language", "zh-CN,zh;q=0.8");
            client.DefaultRequestHeaders.Add("Origin", "http://ehire.51job.com");

            var postdata = new FormUrlEncodedContent(new Dictionary<string, string>{
                {"ctmName","杭州恩牛"},
                {"userName","杭州恩牛"},
                {"password","en007007"},
                {"checkCode",""},
                {"oldAccessKey",oldAccessKey},
                {"langtype","Lang=&amp;Flag=1"},
                {"isRememberMe","false"},
                {"sc",sc},
                {"ec",ec}
            });

            Console.WriteLine(postdata.ReadAsStringAsync().Result);

            HttpResponseMessage rs = client.PostAsync("https://ehirelogin.51job.com/Member/UserLogin.aspx", postdata).Result;
            string res = rs.Content.ReadAsStringAsync().Result;

            Console.WriteLine();
            Console.ReadLine();
        }
    }
}
